Handling Keyboard Equivalents
Keyboard equivalents of menu commands allow the user to invoke a menu command from the keyboard. You can determine if the user chose the keyboard equivalent of a menu command by examining the event record for a key-down event. If the user pressed the Command key in combination with another character, you can then determine if this combination maps to a known Command-key equivalent by calling the Menu Manager functionMenuKey
. Listing 8-5 shows the Venn Diagrammer application'sDoKeyDown
procedure, which handles key-down events and determines if a keyboard equivalent was pressed.Listing 8-5 Handling Command-key equivalents
PROCEDURE DoKeyDown (myEvent: EventRecord); VAR myKey: char; BEGIN myKey := chr(BAnd(myEvent.message, charCodeMask)); IF (BAnd(myEvent.modifiers, CmdKey) <> 0) THEN BEGIN DoMenuAdjust; DoMenuCommand(MenuKey(myKey)); END; END;TheDoKeyDown
procedure first extracts the pressed key from themessage
field of the event record and then examines themodifiers
field to determine whether the Command key was also pressed. If so, the application first adjusts its menus and then calls theDoMenuCommand
procedure defined in Listing 8-3 on page 157. In turn,DoKeyDown
passes toDoMenuCommand
the value returned from theMenuKey
function. If the key combination pressed by the user is not the keyboard equivalent of any currently enabled menu item, thenMenuKey
sets the high-order word of its return value to 0.
Several keyboard equivalents (listed in Table 8-1) are reserved for common commands in the File and Edit menus. If your application supports these commands, you should assign these equivalents to the specified commands. Otherwise, you should ignore these keyboard equivalents.
- Note
- The Venn Diagrammer application does not accept any text input from the user. As a result, the
DoKeyDown
procedure shown in Listing 8-5 doesn't need anELSE
clause to handle keypresses in which the Command key is not held down.![]()
Table 8-1 Table 8-1 Reserved keyboard equivalents
Keys Command Menu graphics/Command-key_Symbol_Prop.jpg
-A
Select All Edit graphics/Command-key_Symbol_Prop.jpg
-C
Copy Edit graphics/Command-key_Symbol_Prop.jpg
-N
New File graphics/Command-key_Symbol_Prop.jpg
-O
Open... File graphics/Command-key_Symbol_Prop.jpg
-P
Print... File graphics/Command-key_Symbol_Prop.jpg
-Q
Quit File graphics/Command-key_Symbol_Prop.jpg
-S
Save File graphics/Command-key_Symbol_Prop.jpg
-V
Paste Edit graphics/Command-key_Symbol_Prop.jpg
-W
Close File graphics/Command-key_Symbol_Prop.jpg
-X
Cut Edit graphics/Command-key_Symbol_Prop.jpg
-Z
Undo Edit
- IMPORTANT
- You should never assign the keyboard equivalents listed in Table 8-1 to other menu commands. This helps ensure predictable behavior among all applications.
![]()